<template>
  <div class="AddressCard">
    <div class="Address_info">
      <p class="info_name">
        收货人：{{ item.real_name + "&nbsp;&nbsp;" + item.phone }}
      </p>
      <p class="info_site">
        收货地址：{{ item.province + item.city + item.district + item.detail }}
      </p>
    </div>
    <div class="operate">
      <div class="operate_setdefault" @click="setDefault(item.id)">
        <van-checkbox
          v-model="checked"
          checked-color="#ee0a24"
          icon-size="0.35rem"
          ref="checkbox"
          >设为默认</van-checkbox
        >
      </div>
      <div class="operate_edit">
        <label @click="changeAdress(item.id)">
          <van-icon name="edit" size="0.35rem" />
          编辑
        </label>
        <label @click="delAddress(item.id)">
          <van-icon name="delete-o" size="0.35rem" />
          删除
        </label>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: ["item", "elseCheckFlag"],
  data() {
    return {
      checked: false,
    };
  },
  methods: {
    setDefault(id) {
      if (this.checked) {
        this.$refs.checkbox.toggle(true);
        return;
      }
      this.$emit("changeDefault", !this.elseCheckFlag);
      this.axios
        .post("/api/address/default/set", {
          id,
        })
        .then((response) => {
          console.log("设为默认地址", response.data.msg);
        });
    },
    delAddress(id) {
      this.$toast("删除成功!");
      setTimeout(() => {
        this.axios
          .post("/api/address/del", {
            id,
          })
          .then((response) => {
            if (response.data.status == 200) {
              this.$emit("anewGetList");
            }
          });
      }, 2000);
    },
    changeAdress(id) {
      this.$router.push({
        path: "/user/add_address",
        query: {
          id,
        },
      });
    },
  },
  watch: {
    elseCheckFlag() {
      this.checked = false;
    },
  },
  created() {
    if (this.item.is_default == 1) {
      this.checked = true;
    }
  },
};
</script>

<style lang="less">
.AddressCard {
  width: 100%;
  height: 110px;
  padding: 0 15px;
  background-color: #fff;
  position: relative;
  display: flex;
  flex-direction: column;
  margin-top: 8px;
  &:first-child {
    margin-top: 0px;
  }

  .Address_info {
    width: 100%;
    padding: 15px 0;
    border-bottom: 1px solid #f4f4f4;
    .info_name,
    .info_site {
      font-size: 14px;
    }
    .info_name {
      font-weight: bold;
      margin-bottom: 8px;
    }
  }
  .operate {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .operate_setdefault {
      font-size: 14px;
    }
    .operate_edit {
      font-size: 14px;
      label {
        margin-left: 15px;
      }
      .van-icon {
        vertical-align: -2px;
      }
    }
  }
}
</style>